package org.wiztools.jerseyjaxrs;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

/**
 *
 * @author subhash
 */
@Produces(MediaType.TEXT_PLAIN)
@Provider
public class TextPlainMessageBodyWriter implements MessageBodyWriter<HelloBean> {

    public boolean isWriteable(Class<?> type, Type type1, Annotation[] antns, MediaType mt) {
        return true;
    }

    public long getSize(HelloBean t, Class<?> type, Type type1, Annotation[] antns, MediaType mt) {
        try{
            return t.toString().getBytes("UTF-8").length;
        }
        catch(UnsupportedEncodingException ex) {
            throw new RuntimeException(ex);
        }
    }

    public void writeTo(HelloBean t, Class<?> type, Type type1, Annotation[] antns, MediaType mt, MultivaluedMap<String, Object> mm, OutputStream out) throws IOException, WebApplicationException {
        out.write(t.toString().getBytes("UTF-8"));
    }
    
}
